package com.guosheng.controller;


import com.guosheng.entity.Student;
import com.guosheng.service.EmpOnService;
import com.guosheng.service.redis.CacheService;
import com.guosheng.util.Signature;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmpController {


    @Autowired
    private EmpOnService empOnService;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/redistest")
    public Object redisTest(){
        return null;
    }

    @RequestMapping("/searchemplist")
    public Object searchEmpList(){
        Object obj = empOnService.searchEmpList(null);

        return obj;
    }

    @RequestMapping("/testcacheservice")
    public String testCacheService(){
        try {
            cacheService.set("guo","sheng");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cacheService.get("guo");
    }

    @RequestMapping("/testnetsfjson")
    public Object testNetSfJson(){
        Student s = new Student();
        s.setId(100);
        List<Integer> list = new ArrayList<Integer>();
        list.add(111);
        list.add(222);
        list.add(333);
        list.add(444);
        s.setList(list);
        JSONObject json = JSONObject.fromObject(s);
        System.out.println(json);
       Student sr = (Student)JSONObject.toBean(JSONObject.fromObject(json),Student.class);
        List<Student> result = new ArrayList<Student>();
        result.add(sr);
        return result;
    }

    @RequestMapping(value = "/testauth", method= RequestMethod.POST)
    public String testAuth(HttpServletRequest request,String authKey,String sign,String info)throws Exception{
        String reqTime = request.getHeader("ReqTime-Time");
        String token ="guoshengguosheng";
        String check = check(reqTime,info,token,sign);
        System.out.println("服务端：" + check);
        return check;
    }

    /**
     * 校验参数和签名
     * @param rt
     * @param info
     * @param token
     * @param sign
     * @return
     */
    private String check(String rt,String info,String token,String sign)throws Exception{
        if(StringUtils.isEmpty(rt))return "头部时间为空";
        if(StringUtils.isEmpty(info)) return "信息为空";
        if(StringUtils.isEmpty(token)) return "token为空";
        if(StringUtils.isEmpty(sign)) return "签名为空";
        long serverTime = Calendar.getInstance().getTimeInMillis();
        long clientTime = Long.parseLong(rt);

        long flag = serverTime - clientTime;
        if(flag < 0 || flag > 1000) return "时间错误";

        String allStr = info + rt + token;
        String mdd5 = Signature.md5(allStr);
        if(sign.equals(mdd5)){
            System.out.println("服务端未签名时为：" + allStr);
            System.out.println("服务端签名之后为：" + mdd5);
            return "签名成功";
        }
        return "签名错误";
    }
}
